package com.ynet.middleground.approve.conf;

import javax.sql.DataSource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import com.ynet.middleground.approve.cmd.CustomProcessDiagramGenerator;
import com.ynet.middleground.approve.identity.CustomGroupEntityManager;
import com.ynet.middleground.approve.identity.CustomUserEntityManager;

/**
 * 使用Java类完成配置文件
 *
 * @author yangbh
 */
@Configuration
public class ActivitiConfig {

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private CustomProcessDiagramGenerator customProcessDiagramGenerator;

    /**
     * 初始化配置，将创建25张表
     *
     * @return
     */
    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration(
        @Qualifier("dataSource") DataSource dataSource,
        @Qualifier("customUserEntityManager") CustomUserEntityManager customUserEntityManager,
        @Qualifier("customGroupEntityManager") CustomGroupEntityManager customGroupEntityManager) {
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setUserEntityManager(customUserEntityManager);
        configuration.setGroupEntityManager(customGroupEntityManager);
        configuration.setTransactionManager(platformTransactionManager);
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        configuration.setAsyncExecutorActivate(false);
        configuration.setProcessDiagramGenerator(customProcessDiagramGenerator);
        return configuration;

    }

    /**
     * 创建引擎
     *
     * @return ProcessEngine
     */
    @Bean
    public ProcessEngine processEngine(
        @Qualifier("springProcessEngineConfiguration") SpringProcessEngineConfiguration springProcessEngineConfiguration) {
        return springProcessEngineConfiguration.buildProcessEngine();
    }

}
